package com.example.jiangkt.ergongyi.util;

import android.app.Application;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketTimeoutException;

/**
 * 所有的activity分别new一个线程共用该socket
 * Created by jiangkt on 2018/3/7.
 */

public class SocketUtil extends Application{
    private Socket socket;
    private BufferedReader bufferedReader = null;
    private OutputStream outputStream = null;

    public void init() {
        try {
            //云服务器120.79.145.80
            //本地服务器192.168.191.1
            this.socket = new Socket("120.79.145.80", 6000);
//            this.socket.connect(new InetSocketAddress("192.168.191.1", 30000), 6000);

            //不要设置超时时间，不然socketUtil.getBufferedReader().readLine()会报错，血
//            this.socket.setSoTimeout(10000);
            //获取输入流
            this.bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //获取输出流
            this.outputStream = socket.getOutputStream();
        } catch (SocketTimeoutException e) {
            if (AlertShow.dialog != null) {
                AlertShow.dialog.dismiss();
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        } catch (IOException e) {
            if (AlertShow.dialog != null) {
                AlertShow.dialog.dismiss();
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public BufferedReader getBufferedReader() {
        return bufferedReader;
    }

    public void setBufferedReader(BufferedReader bufferedReader) {
        this.bufferedReader = bufferedReader;
    }

    public OutputStream getOutputStream() {
        return outputStream;
    }
}

